package com.pb.kanivets.bki.spring.web.controllers;


import com.pb.kanivets.bki.core.entities.Client;
import com.pb.kanivets.bki.core.services.IClientService;
import com.pb.kanivets.bki.core.wrappers.ListWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/clients")
public class ClientsController {

    @Autowired
    private IClientService clientService;

    @RequestMapping("/clients.json")
    public ListWrapper list() {
        System.out.println("listClients");        
        return new ListWrapper(clientService.listClients());
    }

    @RequestMapping("/add")
    public Client add(@RequestBody Client client) {
        clientService.addClient(client);
        return client;
    }

    @RequestMapping("/update")
    public Client update(@RequestBody Client client) {
        clientService.updateClient(client);
        return client;
    }

    @RequestMapping("/delete")
    public Client delete(@RequestBody Client client) {
        clientService.addClient(client);
        return client;
    }

    @RequestMapping("/{id}")
    public Client getClient(@PathVariable int id) {
        return clientService.getClient(id);
    }

    @RequestMapping("/search/byinn")
    public Client searchByInn(@RequestBody String inn) {
        return clientService.searchByInn(inn);
    }

    @RequestMapping("/search/bypassp")
    public Client searchByPassp(@RequestBody String passp) {
        return clientService.searchByPassp(passp);
    }

    @RequestMapping("/search/byfio")
    public ListWrapper searchByPassp(@RequestBody String sName, @RequestBody String fName, @RequestBody String mName) {
        return new ListWrapper(clientService.searchByFIO(sName, fName, mName));
    }

}
